<?php
/**
 * Created by PhpStorm.
 * User: ZhangYi
 * Date: 2011-1-13
 * Time: 15:39:50
 * To change this template use File | Settings | File Templates.
 */

class SearchTestTask extends sfBaseTask
{
    protected function configure()
    {
        parent::configure();
        $this->addOptions(array(new sfCommandOption('query', null,
                              sfCommandOption::PARAMETER_REQUIRED, 'query string', null)));
        $this->namespace = 'ieasy';
        $this->name = 'searchTest';
    }


    protected function execute($arguments = array(), $options = array())
    {

        //                $this->testScws();

        sfConfig::set('sf_environment', 'prod');
        $querystr = $options['query'];
        if ($querystr != null) {
            $NewsIndex = IEasySearch::getLuceneIndex('All');
            print_r($NewsIndex);
            //            print_r(Zend_Search_Lucene_Analysis_Analyzer::getDefault());

            $querystr = IEasySearch::getContentWithOutHtmlTags($querystr);
            print_r($querystr);

            $query = Zend_Search_Lucene_Search_QueryParser::parse($querystr, 'utf-8');
            //            print_r($query);
            $hits = $NewsIndex->find($query);

            echo count($hits) . "\n";
            foreach ($hits as $hit) {
                echo "id:" . $hit->id . " title:" . $hit->title . "\n";
            }
        }
    }


    private function testScws()
    {
        $so = scws_new();
        $so->set_charset('utf-8');
        $so->set_dict('/usr/local/scws/etc/dict.utf8.xdb');
        $so->set_multi(7);
        // 这里没有调用 set_dict 和 set_rule 系统会自动试调用 ini 中指定路径下的词典和规则文件
        $so->send_text("公告牌年度评选揭晓 LadyGaGa当选年度艺人");
        //        print_r($so->get_result());
        while ($tmp = $so->get_result())
        {
            print_r($tmp);
            //            foreach($tmp as $word)
            //            {
            //                echo $word['word'];
            //            }
            //            echo "\n";
            //            echo $tmp[0]['word']." \n";
        }
        $so->close();
    }
}
